<template>
    <div class="code-generate-page">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
            <el-form-item>
                <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="getDataList()">查询</el-button>
                <!-- TODO 留一个注释以后参考用 -->
                <!-- <el-button v-if="isAuth('admin:user:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
                <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="dataList" border v-loading="dataListLoading"
            style="width: 100%;">
            <el-table-column prop="id" header-align="center" align="center" label="id" width="80"/>
            <el-table-column prop="username" header-align="center" align="center" label="用户名"/>
            <el-table-column prop="status" header-align="center" align="center" label="状态" :formatter="statusFormatter"/>
            <el-table-column prop="createDate" header-align="center" align="center" label="创建时间"/>
            <el-table-column prop="updateDate" header-align="center" align="center" label="更新时间"/>
            <el-table-column fixed="right" header-align="center" align="center" width="170" label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="addOrUpdateHandle(scope.row)">修改</el-button>
                    <el-button type="primary" size="mini" @click="grantRole(scope.row.id)">分配角色</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"/>
        <!-- 分配角色 -->
        <user-grant-role v-if="grantRoleVisible" ref="userGrantRole" @refreshDataList="getDataList"/>
    </div>
</template>

<script>
import AddOrUpdate from './user-add-or-update'
import UserGrantRole from './user-grant-role'
export default {
    data() {
        return {
            dataForm: {
                key: ''
            },
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalPage: 0,
            dataListLoading: false,
            addOrUpdateVisible: false,
            grantRoleVisible: false,
        }
    },
    components: {
        AddOrUpdate,
        UserGrantRole
    },
    activated() {
        this.getDataList()
    },
    methods: {
        // 获取数据列表
        getDataList() {
            this.dataListLoading = true
            this.$http({
                url: '/member/admin/account/listAccountInfo',
                method: 'post',
                data: {
                    'key': this.dataForm.key,
                    "pageInfo": {
                        "pageNum": this.pageIndex,
                        'pageSize': this.pageSize
                    }
                }
            }).then((response) => {
                if (response && response.code == "0") {
                    this.dataList = response.data
                    this.totalPage = response.total
                } else {
                    this.dataList = []
                    this.totalPage = 0
                }
                this.dataListLoading = false
            })
        },
        // 每页数
        sizeChangeHandle(val) {
            this.pageSize = val
            this.pageIndex = 1
            this.getDataList()
        },
        // 当前页
        currentChangeHandle(val) {
            this.pageIndex = val
            this.getDataList()
        },
        // 新增 / 修改
        addOrUpdateHandle(data) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(data)
            })
        },
        // 分配角色
        grantRole(id){
            this.grantRoleVisible = true;
            this.$nextTick(() => {
                this.$refs.userGrantRole.init(id)
            })
        },
        statusFormatter(row) {
            switch (row.status) {
                case 1: return "正常";
                case 2: return "禁用";
                default: return "未知状态";
            }
        }
    }
}
</script>
